1 00:00:00,580 --> 00:00:01,270 Okay. 2 00:00:01,270 --> 00:00:06,580 In this lecture, we're going to quickly set up a collision group for players in our game to avoid player 3 00:00:06,580 --> 00:00:07,300 collisions. 4 00:00:07,300 --> 00:00:10,210 Now we should be well versed with collision groups at this point. 5 00:00:10,210 --> 00:00:13,270 So this will be a piece of cake to implement in our project. 6 00:00:13,540 --> 00:00:18,160 We can go ahead and head to our model tab and open up collision groups and create a new collision group 7 00:00:18,160 --> 00:00:18,580 in here. 8 00:00:18,580 --> 00:00:24,490 I'm just going to call it players, and we'll make sure to have players not collide with other players. 9 00:00:24,730 --> 00:00:29,950 Now that we have our new collision group, we can head to Server Script service and add a new module, 10 00:00:29,950 --> 00:00:31,390 script or service. 11 00:00:31,540 --> 00:00:36,040 And I'm just going to call this our collision Group service. 12 00:00:37,290 --> 00:00:39,510 We'll use that same template we've been using before. 13 00:00:39,540 --> 00:00:42,780 I'll rename this to collision Group service. 14 00:00:43,320 --> 00:00:46,620 And inside of here we're going to need the player service. 15 00:00:50,090 --> 00:00:51,890 And we should be good to go. 16 00:00:51,890 --> 00:00:59,210 So what we're going to need is a function to listen for when a character is added to the game. 17 00:00:59,630 --> 00:01:02,180 We'll pass a character to this function. 18 00:01:02,210 --> 00:01:07,460 We're also going to want to listen for when a descendant is added to our player's character. 19 00:01:07,460 --> 00:01:10,910 So we'll call it on, descendant added. 20 00:01:11,060 --> 00:01:15,950 And then we'll also want to listen to when a descendant is removed from our player's character. 21 00:01:15,950 --> 00:01:20,360 That way we remove the collision group off of that particular instance. 22 00:01:20,360 --> 00:01:27,560 So on descendant removed and we'll pass a descendant to this function as well as to this function. 23 00:01:28,240 --> 00:01:31,060 And then we'll also create a function to listen for. 24 00:01:31,060 --> 00:01:36,160 When a player is added to the game, we'll pass a player to this function. 25 00:01:36,160 --> 00:01:42,430 And then with this player we'll listen to their character added event, and we'll connect the on character 26 00:01:42,430 --> 00:01:44,800 added function to that event. 27 00:01:46,060 --> 00:01:51,880 And then for the initialize function, all we're going to need to do is loop through every single player 28 00:01:51,880 --> 00:01:53,110 that is inside of our game. 29 00:01:53,110 --> 00:01:56,500 So in players get players. 30 00:01:57,290 --> 00:02:00,830 And we'll call our on player added and pass that player to the function. 31 00:02:00,830 --> 00:02:08,000 And then we're also going to connect to the players dot player, added event and connect on player added. 32 00:02:09,250 --> 00:02:14,290 So when a character is added to a specific player, what we want to do is loop through every single 33 00:02:14,290 --> 00:02:17,500 descendant inside of that character. 34 00:02:17,500 --> 00:02:21,790 So in character, get descendants. 35 00:02:22,590 --> 00:02:26,940 What we want to do is we'll use our en descendant added function. 36 00:02:26,940 --> 00:02:30,570 So we'll call en descendant added and pass that particular descendant. 37 00:02:31,170 --> 00:02:37,140 And then for this character, we want to go ahead and connect to the descendant added event as well 38 00:02:37,140 --> 00:02:39,090 as the descendant removing event. 39 00:02:39,090 --> 00:02:46,770 So we'll connect en descendant added and then we'll connect to the descendant removing event. 40 00:02:48,460 --> 00:02:50,440 On the Senate removed. 41 00:02:51,220 --> 00:02:55,930 And then all we're going to do in this function is we're going to make sure that this descendant is 42 00:02:55,930 --> 00:02:56,710 a base part. 43 00:02:56,710 --> 00:03:00,670 So is a base part. 44 00:03:01,650 --> 00:03:05,040 If it is not a bass part, then we're just going to return. 45 00:03:05,040 --> 00:03:11,700 Otherwise we can set this descendant collision group property equal to our players collision group. 46 00:03:12,880 --> 00:03:16,420 And then we're going to copy this here and do the same thing for descendant removing. 47 00:03:16,420 --> 00:03:20,500 But this time we want to set the collision group back to the default collision group. 48 00:03:21,040 --> 00:03:24,850 And just like that we're done with setting up collision groups for our game. 49 00:03:24,850 --> 00:03:29,260 So let's go ahead and start up a two player server and see if it works. 50 00:03:29,830 --> 00:03:30,520 All right here. 51 00:03:30,520 --> 00:03:34,990 I've got my two players and let's see if I can collide with each other. 52 00:03:34,990 --> 00:03:36,400 So if I go and walk. 53 00:03:37,540 --> 00:03:40,870 Actually looks like we're still colliding for some reason. 54 00:03:40,870 --> 00:03:42,070 That's a problem. 55 00:03:42,070 --> 00:03:45,640 So let's go ahead and see what we did wrong in our code. 56 00:03:46,420 --> 00:03:48,550 And in fact, we did get an error. 57 00:03:48,550 --> 00:03:53,050 And our error is down here because I accidentally made a little typo. 58 00:03:53,080 --> 00:03:57,550 I put a capital L there instead of a lowercase l, so that's why it didn't work. 59 00:03:57,550 --> 00:04:01,900 So make sure your get players call does not have a capital L in it. 60 00:04:01,900 --> 00:04:05,410 And now we should be able to test with the two player server. 61 00:04:05,440 --> 00:04:08,080 Okay let's go ahead and try that one more time. 62 00:04:08,080 --> 00:04:13,750 If I go and touch my player, as you can see we no longer have collisions with our players. 63 00:04:13,750 --> 00:04:15,550 Very easy to implement. 64 00:04:16,400 --> 00:04:21,380 Now our project should be complete, so I'll go ahead and see you in the next lecture.